package com.njzxw.fs.ftp;

import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.Authority;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.usermanager.impl.BaseUser;
import org.apache.ftpserver.usermanager.impl.WritePermission;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.Banner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
@Configuration
public class FtpApplication implements CommandLineRunner,WebMvcConfigurer {

    @Value("${my.ftp.user}")
    private String userName;

    @Value("${my.ftp.password}")
    private String password;

    @Value("${my.ftp.dir}")
    private String dir;

    @Value("${my.ftp.port}")
    private Integer port;

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(FtpApplication.class);
        app.setBannerMode(Banner.Mode.OFF);
        app.run(args);
    }

    public void addResourceHandlers(ResourceHandlerRegistry registry){
        if("dir".equals(dir)){
            dir = System.getProperty("user.dir")+"/ftp";
        }
        System.out.println("???11???:"+dir);
        File fileDir = new File(dir);
        if(!fileDir.exists()){
            fileDir.mkdirs();
        }

        registry.addResourceHandler("/**").addResourceLocations(ResourceUtils.FILE_URL_PREFIX+dir);
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println(System.getProperty("user.dir"));

        FtpServerFactory serverFactory = new FtpServerFactory();

        ListenerFactory factory = new ListenerFactory();
        //设置监听端口
        factory.setPort(port);

        //替换默认监听
        serverFactory.addListener("default", factory.createListener());

        //用户名
        BaseUser user = new BaseUser();
        user.setName(userName);
        //密码 如果不设置密码就是匿名用户
        user.setPassword(password);
        if("dir".equals(dir)){
            dir = System.getProperty("user.dir")+"/ftp";
        }
        System.out.println("??????:"+dir);
        File fileDir = new File(dir);
        if(!fileDir.exists()){
            fileDir.mkdirs();
        }

        //用户主目录
        user.setHomeDirectory(dir);

        List<Authority> authorities = new ArrayList<Authority>();
        //增加写权限
        authorities.add(new WritePermission());
        user.setAuthorities(authorities);

        //增加该用户
        try {
            serverFactory.getUserManager().save(user);
            /**
             * 也可以使用配置文件来管理用户
             */
//          PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
//          userManagerFactory.setFile(new File("users.properties"));
//          serverFactory.setUserManager(userManagerFactory.createUserManager());

            FtpServer server = serverFactory.createServer();
            server.start();
        } catch (FtpException e) {
            e.printStackTrace();
        }
    }
}
